package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsInspectRegisterInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntImageInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsInspectRegisterInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntImageInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.InspectRegisterInfo;
import cc.rengu.igas.bsps.common.entity.MchntImageInfoBean;
import cc.rengu.igas.bsps.common.enums.InspectCollectTypeEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.BankManagerService;
import cc.rengu.igas.bsps.facade.bean.InspectCollectInfoBean;
import cc.rengu.igas.bsps.facade.bean.MchntExpandImage;
import cc.rengu.igas.bsps.facade.request.InspectNeedAuditMchntQueryRequest;
import cc.rengu.igas.bsps.facade.response.InspectNeedAuditMchntQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020.04.09
 */
public class InspectNeedAuditMchntQueryService extends BankManagerService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        InspectNeedAuditMchntQueryRequest inspectNeedAuditMchntQueryRequest = new InspectNeedAuditMchntQueryRequest();
        ConvertUtil.convertOutput(inspectNeedAuditMchntQueryRequest);
        return inspectNeedAuditMchntQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        InspectNeedAuditMchntQueryRequest inspectNeedAuditMchntQueryRequest = (InspectNeedAuditMchntQueryRequest) request;
        /*获取请求参数信息:法人行号，用户标识，拓展经理工号，巡检任务编号，商户号*/
        String instId = inspectNeedAuditMchntQueryRequest.getHeader().getInstId();
        String userId = inspectNeedAuditMchntQueryRequest.getUserId();
        String managerId = inspectNeedAuditMchntQueryRequest.getManagerId();
        String inspectTaskNo = inspectNeedAuditMchntQueryRequest.getInspectTaskNo();
        String mchntNo = inspectNeedAuditMchntQueryRequest.getMchntNo();
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(managerId) || StringUtil.isEmptyOrNull(inspectTaskNo) || StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("待审核巡检任务详情查询必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*查询巡检任务采集信息*/
        List<InspectCollectInfoBean> inspectCollectInfoList = new ArrayList<>();
        BspsInspectRegisterInfoMapper bspsInspectRegisterInfoMapper = new BspsInspectRegisterInfoMapperImpl();
        List<InspectRegisterInfo> inspectRegisterInfos = bspsInspectRegisterInfoMapper.selectInspectRegisterInfoByMchntNo(instId, inspectTaskNo, mchntNo);
        if (null != inspectRegisterInfos && !inspectRegisterInfos.isEmpty()) {
            inspectRegisterInfos.forEach(item -> convInspcetRegisterInfo(item, inspectCollectInfoList));
        }
        /*查询商户入驻影响信息*/
        List<MchntExpandImage> mchntExpandImageList = new ArrayList<>();
        BspsMchntImageInfoMapper bspsMchntImageInfoMapper = new BspsMchntImageInfoMapperImpl();
        List<MchntImageInfoBean> mchntImageInfos = bspsMchntImageInfoMapper.selectBspsMchntSignImageInfoByMchntNo(instId, mchntNo);
        if (!CollectionUtils.isEmpty(mchntImageInfos)) {
            mchntImageInfos.forEach(item -> convMchntIamgeInfo(item, mchntExpandImageList));
        }
        /*响应报文赋值*/
        InspectNeedAuditMchntQueryResponse inspectNeedAuditMchntQueryResponse = new InspectNeedAuditMchntQueryResponse();
        inspectNeedAuditMchntQueryResponse.setInspectCollectInfoList(inspectCollectInfoList);
        inspectNeedAuditMchntQueryResponse.setMchntExpandImageList(mchntExpandImageList);
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<InspectNeedAuditMchntQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(inspectNeedAuditMchntQueryResponse);
        return bizResponse;
    }

    /*巡检任务详细采集信息转换
     * inspectRegisterInfo 采集信息
     **/
    private void convInspcetRegisterInfo(InspectRegisterInfo inspectRegisterInfo, List<InspectCollectInfoBean> inspectCollectInfoList) {
        InspectCollectInfoBean inspectCollectInfo = new InspectCollectInfoBean();
        /* 采集信息id赋值*/
        inspectCollectInfo.setId(inspectRegisterInfo.getId());
        /*采集信息标识赋值*/
        inspectCollectInfo.setCollectId(inspectRegisterInfo.getCollectId());
        /*采集信息类型赋值*/
        inspectCollectInfo.setCollectType(inspectRegisterInfo.getCollectType());
        /*采集信息描述赋值*/
        inspectCollectInfo.setCollectDesc(inspectRegisterInfo.getCollectDesc());
        /*采集信息赋值*/
        if ("2".equals(inspectRegisterInfo.getCollectType())) {
            inspectCollectInfo.setCollectValue(Base64.encodeBase64String(inspectRegisterInfo.getCollectValue().getBytes()));
        } else {
            inspectCollectInfo.setCollectValue(inspectRegisterInfo.getCollectValue());
        }

        if (InspectCollectTypeEnum.PICTURE.getCollectCode().equals(inspectRegisterInfo.getCollectType())) {
            /*采集影像经度赋值*/
            inspectCollectInfo.setCollectImageLng(inspectRegisterInfo.getCollectImageLng());
            /*采集影像纬度赋值*/
            inspectCollectInfo.setCollectImageLat(inspectRegisterInfo.getCollectImageLat());
        }
        inspectCollectInfoList.add(inspectCollectInfo);
    }

    /*商户入驻影像信息转换
     *mchntImageInfo 商户入驻影像资料
     **/
    private void convMchntIamgeInfo(MchntImageInfoBean mchntImageInfo, List<MchntExpandImage> mchntExpandImageList) {
        MchntExpandImage mchntExpandImage = new MchntExpandImage();
        /*影像类型赋值*/
        mchntExpandImage.setImageType(mchntImageInfo.getImageType());
        /*影像信息描述赋值*/
        mchntExpandImage.setImageDesc(mchntImageInfo.getImageDesc());
        /*影像资料序号赋值*/
        mchntExpandImage.setImageIndex(String.valueOf(mchntImageInfo.getImageIndex()));
        /*影像路径赋值*/
        if (!StringUtil.isEmptyOrNull(mchntImageInfo.getImageLocalPath())) {
            mchntExpandImage.setImagePath(Base64.encodeBase64String(mchntImageInfo.getImageLocalPath().getBytes()));
        }
        mchntExpandImageList.add(mchntExpandImage);
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        InspectNeedAuditMchntQueryResponse inspectNeedAuditMchntQueryResponse = (InspectNeedAuditMchntQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(inspectNeedAuditMchntQueryResponse);
    }
}
